package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EmployeesGUI extends JFrame implements ActionListener {
    private JPanel panelEmployees;
    private JPanel panelCreateEmployee;
    private JPanel panelFindEmployee;
    private JPanel panelFindEmployee2;
    private JPanel panelUpdateEmployee;
    private JPanel panelUpdateEmployee2;
    private JPanel panelDeleteEmployee;
    private JPanel panelDeleteEmployee2;
    private EmployeeController employeeController;
    private Container container;
    private int sessionId;

    public EmployeesGUI(int sessionId) {
        panelEmployees = new JPanel(new GridBagLayout());
        panelCreateEmployee = new JPanel(new GridBagLayout());
        panelFindEmployee = new JPanel(new GridBagLayout());
        panelFindEmployee2 = new JPanel(new GridBagLayout());
        panelUpdateEmployee = new JPanel(new GridBagLayout());
        panelUpdateEmployee2 = new JPanel(new GridBagLayout());
        panelDeleteEmployee = new JPanel(new GridBagLayout());
        panelDeleteEmployee2 = new JPanel(new GridBagLayout());
        employeeController = new EmployeeController();
        this.sessionId = sessionId;

        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400,470);
        this.setResizable(false);

        employeesMainMenu();
    }

    public void actionPerformed(ActionEvent e){

    }

    public void makeEmployeesMenu() {
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addEmployee = new JButton("Add Employee");
        addEmployee.setPreferredSize(new Dimension(150,25));
        addEmployee.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    createEmployee();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelEmployees.add(addEmployee, gbc);

        JButton findEmployee = new JButton("Find Employee");
        findEmployee.setPreferredSize(new Dimension(150,25));
        findEmployee.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findEmployee();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelEmployees.add(findEmployee, gbc);

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            JButton updateEmployee = new JButton("Update Employee");
            updateEmployee.setPreferredSize(new Dimension(150,25));
            updateEmployee.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        updateEmployeeFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(5,5,5,5);
            panelEmployees.add(updateEmployee, gbc);

            JButton deleteEmployee = new JButton("Delete Employee");
            deleteEmployee.setPreferredSize(new Dimension(150,25));
            deleteEmployee.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        deleteEmployeeFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.insets = new Insets(5,5,5,5);
            panelEmployees.add(deleteEmployee, gbc);
        }

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                   dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelEmployees.add(back, gbc);
    }

    public void employeesMainMenu()
    {
        makeEmployeesMenu();
        this.getContentPane().add(panelEmployees, BorderLayout.NORTH);
    }

    private void create()
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textDepartment = new JTextField();
        JPasswordField textPassword = new JPasswordField();
        JLabel labelHeader;
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelDepartment;
        JLabel labelPassword;
        JLabel errorMessage;
        JButton create;
        JButton clear;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textPassword.setText("");
        textId.setText("");
        textName.setText("");
        textEmail.setText("");
        textPhoneNumber.setText("");
        textDepartment.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(textPhoneNumber,gbc);

        labelDepartment = new JLabel("Department:", JLabel.RIGHT);
        labelDepartment.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(labelDepartment, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textDepartment.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(textDepartment,gbc);

        labelPassword = new JLabel("Password:", JLabel.RIGHT);
        labelPassword.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(labelPassword,gbc);

        textPassword.setEchoChar('*');
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textPassword.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(textPassword,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateEmployee.add(errorMessage, gbc);

        create = new JButton("Create");
        create.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        employeeController.createEmployee(Integer.parseInt(textId.getText()),
                            textName.getText(),
                            textEmail.getText(),
                            textPhoneNumber.getText(),
                            textDepartment.getText(),
                            textPassword.getText());
                            container = getContentPane();
                    panelCreateEmployee.removeAll();
                    container.removeAll();
                    createEmployee();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                        textPassword.setText("");
                        textId.setText("");
                        textName.setText("");
                        textEmail.setText("");
                        textPhoneNumber.setText("");
                        textDepartment.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        createEmployee();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textPassword.setText("");
                        textId.setText("");
                        textName.setText("");
                        textEmail.setText("");
                        textPhoneNumber.setText("");
                        textDepartment.setText("");
                        errorMessage.setText("Not possible!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        create.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(create,gbc);

        clear = new JButton("Clear");
        clear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelCreateEmployee.removeAll();
                    container.removeAll();
                    createEmployee();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textPassword.setText("");
                    textId.setText("");
                    textName.setText("");
                    textEmail.setText("");
                    textPhoneNumber.setText("");
                    textDepartment.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        clear.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(clear,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    
                    textPassword.setText("");
                    textId.setText("");
                    textName.setText("");
                    textEmail.setText("");
                    textPhoneNumber.setText("");
                    textDepartment.setText("");
                    errorMessage.setText("");
                    
                    container = getContentPane();
                    panelCreateEmployee.removeAll();
                    container.removeAll();
                    employeesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelCreateEmployee.add(back,gbc);
    }

    public void createEmployee()
    {
        create();
        this.getContentPane().add(panelCreateEmployee, BorderLayout.NORTH);
    }

    private void find()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFindEmployee.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findEmployeeSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findEmployee();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("No employee!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelFindEmployee.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    employeesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelFindEmployee.add(back,gbc);
    }

    public void findEmployee()
    {
        find();
        this.getContentPane().add(panelFindEmployee, BorderLayout.NORTH);
    }

    public void findSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textDepartment = new JTextField();
        JPasswordField textPassword = new JPasswordField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelDepartment;
        JLabel labelPassword;
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textName.setEditable(false);
        textEmail.setEditable(false);
        textPhoneNumber.setEditable(false);
        textDepartment.setEditable(false);
        textPassword.setEditable(false);

        textPassword.setText(employeeController.findEmployee(id).getPassword());
        textId.setText(Integer.toString(id));
        textName.setText(employeeController.findEmployee(id).getName());
        textEmail.setText(employeeController.findEmployee(id).getEmail());
        textPhoneNumber.setText(employeeController.findEmployee(id).getPhoneNumber());
        textDepartment.setText(employeeController.findEmployee(id).getDepartment());

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(textPhoneNumber,gbc);

        labelDepartment = new JLabel("Department:", JLabel.RIGHT);
        labelDepartment.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee2.add(labelDepartment, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textDepartment.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(textDepartment,gbc);

        labelPassword = new JLabel("Password:", JLabel.RIGHT);
        labelPassword.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelFindEmployee2.add(labelPassword,gbc);

        textPassword.setEchoChar('*');
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textPassword.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(textPassword,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findEmployee();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(100,25));
        panelFindEmployee2.add(ok,gbc);
    }

    public void findEmployeeSecondWindow(int id)
    {
        findSecondWindow(id);
        this.getContentPane().add(panelFindEmployee2, BorderLayout.NORTH);
    }

    private void updateFirstWindow()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee.add(textId,gbc);
        
        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        updateEmployeeSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        updateEmployeeFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("No employee!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    employeesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee.add(back,gbc);
    }

    private void updateSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textDepartment = new JTextField();
        JPasswordField textPassword = new JPasswordField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelDepartment;
        JLabel labelPassword;
        JButton update;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textPassword.setText(employeeController.findEmployee(id).getPassword());
        textId.setText(Integer.toString(id));
        textName.setText(employeeController.findEmployee(id).getName());
        textEmail.setText(employeeController.findEmployee(id).getEmail());
        textPhoneNumber.setText(employeeController.findEmployee(id).getPhoneNumber());
        textDepartment.setText(employeeController.findEmployee(id).getDepartment());

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(textPhoneNumber,gbc);

        labelDepartment = new JLabel("Department:", JLabel.RIGHT);
        labelDepartment.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee2.add(labelDepartment, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textDepartment.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(textDepartment,gbc);

        labelPassword = new JLabel("Password:", JLabel.RIGHT);
        labelPassword.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateEmployee2.add(labelPassword,gbc);

        textPassword.setEchoChar('*');
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textPassword.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(textPassword,gbc);

        update = new JButton("Update");
        update.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    employeeController.updateEmployee(id, 
                        textName.getText(), 
                        textEmail.getText(), 
                        textPhoneNumber.getText(), 
                        textDepartment.getText(), 
                        textPassword.getText());
                    container = getContentPane();
                    container.removeAll();
                    updateEmployeeFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        update.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(update,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    updateEmployeeFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelUpdateEmployee2.add(cancel,gbc);

    }

    public void updateEmployeeFirstWindow()
    {
        updateFirstWindow();
        this.getContentPane().add(panelUpdateEmployee, BorderLayout.NORTH);
    }

    public void updateEmployeeSecondWindow(int id)
    {
        updateSecondWindow(id);
        this.getContentPane().add(panelUpdateEmployee2, BorderLayout.NORTH);
    }

    private void deleteFirstWindow()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        deleteEmployeeSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        deleteEmployeeFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("No employee!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    employeesMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee.add(back,gbc);
    }

    private void deleteSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textDepartment = new JTextField();
        JPasswordField textPassword = new JPasswordField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelDepartment;
        JLabel labelPassword;
        JButton delete;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textName.setEditable(false);
        textEmail.setEditable(false);
        textPhoneNumber.setEditable(false);
        textDepartment.setEditable(false);
        textPassword.setEditable(false);

        textPassword.setText(employeeController.findEmployee(id).getPassword());
        textId.setText(Integer.toString(id));
        textName.setText(employeeController.findEmployee(id).getName());
        textEmail.setText(employeeController.findEmployee(id).getEmail());
        textPhoneNumber.setText(employeeController.findEmployee(id).getPhoneNumber());
        textDepartment.setText(employeeController.findEmployee(id).getDepartment());

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(textPhoneNumber,gbc);

        labelDepartment = new JLabel("Department:", JLabel.RIGHT);
        labelDepartment.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee2.add(labelDepartment, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textDepartment.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(textDepartment,gbc);

        labelPassword = new JLabel("Password:", JLabel.RIGHT);
        labelPassword.setPreferredSize(new Dimension(100,25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteEmployee2.add(labelPassword,gbc);

        textPassword.setEchoChar('*');
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textPassword.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(textPassword,gbc);

        delete = new JButton("Delete");
        delete.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    employeeController.deleteEmployee(id);
                    container = getContentPane();
                    container.removeAll();
                    deleteEmployeeFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        delete.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(delete,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    deleteEmployeeFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelDeleteEmployee2.add(cancel, gbc);

    }

    private void deleteEmployeeFirstWindow()
    {
        deleteFirstWindow();
        this.getContentPane().add(panelDeleteEmployee, BorderLayout.NORTH);
    }

    private void deleteEmployeeSecondWindow(int id)
    {
        deleteSecondWindow(id);
        this.getContentPane().add(panelDeleteEmployee2, BorderLayout.NORTH);
    }
}